package com.yaodian100.ec.activity;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.ec.activity.entity.ActivityPage;
import com.yaodian100.ec.activity.service.ActivityPageBlockTemplateService;
import com.yaodian100.ec.activity.service.ActivityPageService;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.common.web.AbstractAction;

/**
 * 
 * @author chin.hung
 *
 */
public class ActivityPageAction extends AbstractAction implements Preparable {

	private static final String DEFAULT_MAIN_VISUAL_CONTENT_TYPE = "existingImageFile";
	private static final String RESULT_EDIT_BLOCK = "editBlock";
	private static final String RESULT_EDIT_PAGE = "editPage";
	private static final String RESULT_LIST = "list";
	
	private ActivityPage activityPage = null;
	private String mainVisualContentType = DEFAULT_MAIN_VISUAL_CONTENT_TYPE;
	private List<ActivityPage> pageList;
	
	@Autowired
	private ActivityPageBlockTemplateService blockTemplateService;
	@Autowired
	private ActivityPageService activityPageService;
	
	//查詢條件-活動頁名稱
	private String titleParam;
	private String expectedOnlineDateStParam;
	private String expectedOnlineDateEdParam;
	DateFormat format = new SimpleDateFormat(EcConstant.PATTERN_TIMESTAMP);
	
	// 活動主視覺上傳圖檔
	private File mainVisualImage;
	private String mainVisualImageFileName;
	private String previewImageURL;
	
	private static final long serialVersionUID = 1L;
	
	private boolean gotoBlockDetail = false;
	
	public String createPage() {
		this.activityPage = new ActivityPage();
		this.activityPage.setMainVisualContentType(1);
		return RESULT_EDIT_PAGE;
	}
	
	public String editPage() throws Exception {
		if (this.activityPage == null || this.activityPage.getId() == null) {
			return search();
		}
		this.activityPage = this.activityPageService.get(this.activityPage.getId());
		
		if (activityPage == null) {
			return search();
		}
		
		if (this.activityPage.getMainVisualContentType().intValue() == 2) {
			this.previewImageURL = ActivityPageService.MAIN_VISUAL_IMAGE_WEB_PATH + "/" + this.activityPage.getMainVisualImageFilePath();
		} else {
			this.previewImageURL = null;
		}
		return RESULT_EDIT_PAGE;
	}
	
	public String createBlock() {
		return RESULT_EDIT_BLOCK;
	}
	
	public String search() throws Exception {
		CommonCriteria criteria = new CommonCriteria();
		if (StringUtils.isNotBlank(titleParam)) {
			criteria.addRlike("title", "%"+titleParam);
		}
		
		if (StringUtils.isNotBlank(this.expectedOnlineDateStParam)) {
			criteria.addGe("expectedOnlineDate", format.parse(expectedOnlineDateStParam + " 00:00:00"));
		}
		if (StringUtils.isNotBlank(this.expectedOnlineDateEdParam)) {
			criteria.addLe("expectedOnlineDate", format.parse(expectedOnlineDateEdParam + " 23:59:59"));
		}
		
		String[] sortOrder = new String[] {"modifyDate desc","createDate desc"};
		super.getPager().setTotalSize(this.activityPageService.getListSize(criteria).intValue());
		this.pageList = this.activityPageService.getList(getPager().getPageRecord()*getPager().getCurrentPage(), getPager().getPageRecord(), criteria, sortOrder);
		checkComplete(this.pageList);
		
		return RESULT_LIST;
	}
	
	private void checkComplete(List<ActivityPage> pages) {
		for (ActivityPage page:pages) {
			if (page.getStyleId() != null && page.getExpectedOnlineDate() != null && page.getCustomFileName() != null) {
				page.setComplete(true);
			} else {
				page.setComplete(false);
			}
		}
	}
	
	public String savePage() throws Exception {
		if (this.activityPage.getMainVisualContentType().intValue() == 2) {
			if (this.mainVisualImage != null) {
				String file = this.activityPageService.uploadMainVisualImage(this.mainVisualImage);
				this.activityPage.setMainVisualImageFilePath(file);
			}
		} else {
			this.activityPage.setMainVisualImageFilePath(null);
		}
		this.mainVisualImage = null;
		if (this.activityPage.getId() == null) {
			this.activityPage = this.activityPageService.save(this.activityPage);
			return search();
			
		} else {
			ActivityPage page = this.activityPageService.get(this.activityPage.getId());
			page.setTitle(this.activityPage.getTitle());
			page.setMainVisualContentType(this.activityPage.getMainVisualContentType());
			page.setMainVisualImageUrl(this.activityPage.getMainVisualImageUrl());
			page.setMainVisualImageTargetUrl(this.activityPage.getMainVisualImageTargetUrl());
			if (this.activityPage.getMainVisualContentType().intValue() == 2) {
				if (this.activityPage.getMainVisualImageFilePath()!=null) {
					page.setMainVisualImageFilePath(this.activityPage.getMainVisualImageFilePath());
				}
			} else {
				page.setMainVisualImageFilePath(null);
			}
			page.setMainVisualHtmlFilePath(this.activityPage.getMainVisualHtmlFilePath());
			page.setMainVisualHtmlContent(this.activityPage.getMainVisualHtmlContent());
			this.activityPageService.update(page);
		}
		if (this.activityPage.getMainVisualContentType().intValue() == 2) {
			this.previewImageURL = ActivityPageService.MAIN_VISUAL_IMAGE_WEB_PATH + "/" + this.activityPage.getMainVisualImageFilePath();
		}
		return RESULT_EDIT_PAGE;
	}

	public String saveAndPreviewPage() throws Exception {
		this.savePage();
		return "preview";
	}
	
	public ActivityPage getActivityPage() {
		return this.activityPage;
	}
	public void setActivityPage(ActivityPage activityPage) {
		this.activityPage = activityPage;
	}

	public String getMainVisualContentType() {
		return this.mainVisualContentType;
	}
	public void setMainVisualContentType(String mainVisualContentType) {
		this.mainVisualContentType = mainVisualContentType;
	}

	public ActivityPageBlockTemplateService getBlockTemplateService() {
		return this.blockTemplateService;
	}
	public void setBlockTemplateService(ActivityPageBlockTemplateService blockTemplateService) {
		this.blockTemplateService = blockTemplateService;
	}

	public List<ActivityPage> getPageList() {
		return this.pageList;
	}
	public void setPageList(List<ActivityPage> pageList) {
		this.pageList = pageList;
	}

	public ActivityPageService getActivityPageService() {
		return this.activityPageService;
	}
	public void setActivityPageService(ActivityPageService activityPageService) {
		this.activityPageService = activityPageService;
	}

	public String getTitleParam() {
		return titleParam;
	}

	public void setTitleParam(String titleParam) {
		this.titleParam = titleParam;
	}

	public File getMainVisualImage() {
		return mainVisualImage;
	}

	public void setMainVisualImage(File mainVisualImage) {
		this.mainVisualImage = mainVisualImage;
	}

	public String getMainVisualImageFileName() {
		return mainVisualImageFileName;
	}

	public void setMainVisualImageFileName(String mainVisualImageFileName) {
		this.mainVisualImageFileName = mainVisualImageFileName;
	}

	public String getPreviewImageURL() {
		return previewImageURL;
	}

	public void setPreviewImageURL(String previewImageURL) {
		this.previewImageURL = previewImageURL;
	}

	public boolean isGotoBlockDetail() {
		return gotoBlockDetail;
	}

	public void setGotoBlockDetail(boolean gotoBlockDetail) {
		this.gotoBlockDetail = gotoBlockDetail;
	}

	public String getExpectedOnlineDateStParam() {
		return expectedOnlineDateStParam;
	}

	public void setExpectedOnlineDateStParam(String expectedOnlineDateStParam) {
		this.expectedOnlineDateStParam = expectedOnlineDateStParam;
	}

	public String getExpectedOnlineDateEdParam() {
		return expectedOnlineDateEdParam;
	}

	public void setExpectedOnlineDateEdParam(String expectedOnlineDateEdParam) {
		this.expectedOnlineDateEdParam = expectedOnlineDateEdParam;
	}

	




}